/*
 * Name: LocationActivity
 * Author: Sam Oakley
 * Date: 2/5/2010
 * Version Number: 1.1
 * Description: Displays event location on a map. Uses GMaps API 
 * Changes History: 
 * 1.0 Created class
 * 1.1 Added 'open in google maps' option
 */

package com.blork.rag;

import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class LocationActivity extends MapActivity {
	private EventsData events;
	private SQLiteDatabase db;
	
	private String lat = null;
	private String lon = null;
	
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);

	    Bundle extras = getIntent().getExtras(); 
	    String title = extras.getString("title");
	    setContentView(R.layout.event_map);

	    events = new EventsData(this);
	    db=events.getReadableDatabase();
			Cursor event = db.query("events", new String[] {"location_name, location_long, location_lat"}, 
	                "title LIKE '" + title +"' LIMIT 1", null, null, null, null);
			startManagingCursor(event);
		
		String location = null;

		
		try {
			if (event.moveToFirst()){
				location = event.getString(event.getColumnIndex("location_name"));
				lat = event.getString(event.getColumnIndex("location_lat"));
				lon = event.getString(event.getColumnIndex("location_long"));
			}
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), e.toString(),
   	              Toast.LENGTH_LONG).show();	
		}	    

		int m_lat = (int) (Float.valueOf(lat.trim()).floatValue()*1000000);
		int m_lon = (int) (Float.valueOf(lon.trim()).floatValue()*1000000);
		
	    MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    MapController mapController = mapView.getController();
	    
	    List<Overlay> mapOverlays = mapView.getOverlays();
	    Drawable drawable = this.getResources().getDrawable(R.drawable.arrow);
	    MapOverlay itemizedoverlay = new MapOverlay(drawable, this);
	    
	    GeoPoint point = new GeoPoint(m_lat, m_lon);
	    OverlayItem overlayitem = new OverlayItem(point, title, location);
	
	    itemizedoverlay.addOverlay(overlayitem);
	    mapOverlays.add(itemizedoverlay);
		
	    //If if event isn't DIRECTLY ON TOP OF the Uni, add it to the map as a point of reference
	    if(m_lat != 53380200 && m_lon != -1488090){
		    Drawable uni = this.getResources().getDrawable(R.drawable.university);
		    MapOverlay itemizedoverlay2 = new MapOverlay(uni, this);
		    GeoPoint point2 = new GeoPoint(53380200, -1488090);
		    OverlayItem overlayitem2 = new OverlayItem(point2, "University of Sheffield", "This is where the Uni is.");   
		    itemizedoverlay2.addOverlay(overlayitem2);
		    mapOverlays.add(itemizedoverlay2);
		    mapController.animateTo(point);
		    mapController.setZoom(17);
	    }else{
	    	mapController.animateTo(point);
	    	mapController.setZoom(17);
	    }
    }
    
    /* Creates menu shown when user hits hardware menu button */
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.map_menu, menu);
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
    	String uri = "geo:"+ lat + "," + lon + "?z=17";
    	/* The Google Maps activity interface spec isn't fully defined yet. This could break in 2.2+. */
    	Intent mapIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
    	startActivity(mapIntent);  
        return true;
    } 
    
    public void onDestroy(){
    	super.onDestroy();
    	db.close();
    }
    
   
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
